#include "received.h"
#include "xdebug.h"

RECEIVED*   
received_new()
{
    RECEIVED*   recv = (RECEIVED*)emalloc(sizeof(RECEIVED));
    if (NULL == recv)
        return NULL;

    memset(recv, 0, sizeof(RECEIVED));

    return recv;
}


void        
received_delete(RECEIVED* recv)
{
    if (NULL == recv)
        return ;

    received_clear(recv);

    efree(recv);

}

void        
received_clear(RECEIVED* recv)
{
    if (NULL == recv)
        return ;
    
    SAFE_FREE(recv->From)
    SAFE_FREE(recv->By)
    SAFE_FREE(recv->Via)
    SAFE_FREE(recv->With)
    SAFE_FREE(recv->Id)
    SAFE_FREE(recv->For)
    SAFE_FREE(recv->DateTime)

}



int         
received_set(void* rec, char* member, char* value)
{
    RECEIVED* recv = rec;
    if (recv == NULL)
        return -1;

    if (member == NULL)
        return -2;

    if (value == NULL)
        return -3;

    // notice: xstrdup may be use static char* in some library
    INFO("received set member: %s, val: %s\n", member, value);

    switch(member[0])
    {
    case 'f':
        if (member[1] == 'r'){
            if (recv->From != NULL)
                efree(recv->From);
            recv->From = xstrdup(value);
        }else if (member[1] == 'o'){
            if (recv->For != NULL)
                efree(recv->For);
            recv->For = xstrdup(value);

        }else{
            return -5;
        }
        break;
    case 'b':
        if (recv->By != NULL)
            efree(recv->By);
        recv->By = xstrdup(value);
        break;
    case 'v':
        if (recv->Via != NULL)
            efree(recv->Via);
        recv->Via = xstrdup(value);
        break;
    case 'w':
        if (recv->With != NULL)
            efree(recv->With);
        recv->With = xstrdup(value);
        break;
    case 'i':
        if (recv->Id != NULL)
            efree(recv->Id);
        recv->Id = xstrdup(value);
        break;
    case 'd':
        if (recv->DateTime != NULL)
            efree(recv->DateTime);
        recv->DateTime = xstrdup(value);
        break;
    default:
        return -4;
    }

    return 0;
}



int         
received_parse(RECEIVED* recv, char* data)
{
    int     ret;

    if (recv == NULL)
        return -1;
    if (NULL == data)
        return -2;
  
    received_clear(recv);

    ret = fields_get_pairs_recv(data, recv, received_set);

    return ret;
}


void
received_print(RECEIVED* recv)
{
    if (NULL == recv)
        return ;

    printf("Recevied\n");
    printf("\tFrom: %s\n", recv->From);
    printf("\tBy: %s\n", recv->By);
    printf("\tFor: %s\n", recv->For);
    printf("\tVia: %s\n", recv->Via);
    printf("\tWith: %s\n", recv->With);
    printf("\tId: %s\n", recv->Id);
    printf("\tDate: %s\n", recv->DateTime);
}

int
received_string (RECEIVED *rec, char *save, int slen)
{
    if (NULL == rec)
        return -1;

    return 0;
}


#ifdef MAIN_TEST
int 
main()
{
    int ret;
    char    *test = "Received: from 219.143.47.18 (HELO eyou-4dbc7b3030) (219.143.47.18) by 59.151.4.2 with SMTP; Thu, 24 Aug 2006 14:00:03 +0800";
    
    RECEIVED* recv;

    printf("%s\n", test);
    recv = received_new();

    ret = received_parse(recv, test);
    printf("ret=%d\n", ret);

    received_print(recv);


    return 0;
}


#endif


